﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LL.Business;
using NanoERP.Business;
using NanoERP.Business.Permission;
using NanoERP.Model.Poco;
using NanoERP.Model.SystemMgmt;

namespace NanoERP.Modules.SystemMgmt
{
    public partial class FrmSelectRoleFuncs : FrmSelectFuncsBase
    {
        public FrmSelectRoleFuncs()
        {
            InitializeComponent();
        }

        public SYS_Role Data { get; set; }

        protected override void LoadData()
        {
            if (Data == null)
                return;
            ModuleTree tree = null;
            using (var session = BusinessSessionFactory.NewSession())
            {
                var srv = session.Get<ModuleService>();
                tree = srv.GetModuleTreeForRole(Data.Id);
            }
            BuildNode(treeView1.Nodes[0].Nodes, tree.Children);
            BuildModules(treeView1.Nodes[0].Nodes, tree);
            if (this.treeView1.Nodes[0] != null)
                this.treeView1.Nodes[0].ExpandAll();
        }

        protected override bool SaveData()
        {
            var list = new List<Guid>();
            GetSelectValue(list, treeView1.Nodes[0].Nodes);
            using (var session = BusinessSessionFactory.NewSession(true))
            {
                session.Get<RoleService>().SetRoleRights(Data.Id, list);
                session.Commit();
            }
            return true;
        }

    }
}
